<?php
/**
 * Description: 单题突破\名师精批\申论套题收藏列表
 * User: lizhen
 * Date: 14/10/16
 * Time: 16:03
 */

class GetCollectShenlun implements iController {


    public function output($queryArgs) {
        global $uddb;

        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        $userId = $queryArgs['user_id'];
        $offset = !empty($queryArgs['offset']) ? $queryArgs['offset'] : 0;
        $limit  = !empty($queryArgs['limit']) ? $queryArgs['limit'] : 20;

        if ($limit > 20) $limit = 20;

        $sql = "select * from history_primary_question_review where user_id=:uid and is_collected = 1 order by id desc limit {$offset} , {$limit} ";
        $result = $uddb->getResults($sql, [':uid' => $userId]);

        $data = [];
        if (!empty($result)) {

            foreach ($result as $k => $v) {

                $questionInfo = $this->_getQuestionInfo($v->question_id);
                $data[] = [
                    'user_id'       => (int)$v->user_id,
                    'exercise_id'   => (int)$v->exercise_id,
                    'question_id'   => (int)$v->question_id,
                    'is_collected'  => (int)$v->is_collected,
                    'paper'         => $questionInfo['paper_name'],
                    'question'      => $questionInfo['question']
                ];
            }
        }
        return genOutput(1, ['list' => $data]);
    }

    /**
     * 获取题目信息(试卷名称+小题的问题)
     *
     * @param $questionId
     * @return array
     */
    private function _getQuestionInfo($questionId) {
        global $cmsdb;

        $result = [
            'question' => '',
            'paper_name' => ''
        ];

        $questionInfo = $cmsdb->getRow("select question,paper_id from original_ztk_shenlun_questions where id=:qId",
            [':qId' => $questionId]);

        if (!empty($questionInfo)) {

            $qArr = explode('、',$questionInfo->question);
            $result['question'] = $qArr[0];

            $paperName = $cmsdb->getVar("select name from original_ztk_shenlun_papers where id=:pId",
                [':pId'=>$questionInfo->paper_id]);

            if (!empty($paperName)) $result['paper_name'] = $paperName;
        }
        return $result;
    }

}